#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using Microsoft.Xna.Framework;

namespace SibLib.GameState
{
    public class NestedMenuScreen : MenuScreen
    {
        protected int m_SelectedSubEntry = -1;

        public int SelectedNestedEntry
        {
            get { return m_SelectedSubEntry; }
        }

        public NestedMenuScreen(string menuTitle) :
            base(menuTitle)
        {
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            MenuEntry entry = MenuEntries[m_SelectedEntry];
            if (entry is NestedMenuEntry)
            {
                NestedMenuEntry nestedEntry = (NestedMenuEntry)entry;

                if (nestedEntry.IsExpanded || nestedEntry.IsAlwaysExpanded)
                {
                    // Move to the previous menu entry?
                    if (input.IsMenuUp(ControllingPlayer))
                    {
                        m_SelectedSubEntry--;

                        if (m_SelectedSubEntry < 0)
                        {
                            if (nestedEntry.IsAlwaysExpanded)
                            {
                                m_SelectedEntry--;
                                if (m_SelectedEntry < 0)
                                    m_SelectedEntry = MenuEntries.Count - 1;
                            }
                            else
                            {
                                m_SelectedSubEntry = nestedEntry.ChildEntries.Count - 1;
                            }
                        }

                        PlaySound(CueNameUpDown);
                        return;
                    }

                    // Move to the next menu entry?
                    if (input.IsMenuDown(ControllingPlayer))
                    {
                        m_SelectedSubEntry++;

                        if (m_SelectedSubEntry >= nestedEntry.ChildEntries.Count)
                        {
                            if (nestedEntry.IsAlwaysExpanded)
                            {
                                m_SelectedSubEntry = -1;
                                m_SelectedEntry++;
                                if (m_SelectedEntry >= MenuEntries.Count)
                                    m_SelectedEntry = 0;
                            }
                            else
                            {
                                m_SelectedSubEntry = 0;
                            }
                        }

                        PlaySound(CueNameUpDown);
                        return;
                    }

                    PlayerIndex playerIndex;

                    if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
                    {
                        nestedEntry.ChildEntries[m_SelectedSubEntry].OnSelectEntry(playerIndex);
                        PlaySound(CueNameValid);
                    }
                    else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
                    {
                        nestedEntry.IsExpanded = false;
                        PlaySound(CueNameCancel);
                    }

                    if (ControllingPlayer.HasValue)
                        nestedEntry.ChildEntries[m_SelectedSubEntry].OnCustomInput(input, ControllingPlayer.Value);

                    return;
                }
            }

            base.HandleInput(gameTime, input);
        }

        protected override void OnSelectionChange(int newSelection)
        {
            int delta = newSelection - m_SelectedEntry;
            base.OnSelectionChange(newSelection);

            NestedMenuEntry entry = MenuEntries[m_SelectedEntry] as NestedMenuEntry;
            if (entry != null)
                m_SelectedSubEntry = (delta < 0) ? entry.ChildEntries.Count - 1 : 0;
        }
    }
}